Ext.define('eView.controller.eview.viewmainlistcontroller', {
    extend: 'Ext.app.Controller',
	
	views: [
        'eview.MainList'
    ],
	models: [
		'eview.mainListModel'
	],
	stores: [
		'eview.mainListStore'
	],
	
    init: function() {
		console.log('init main list controller');
        this.control({
            'mainlist button[action=openeview]': {
				click: this.openView
			},
			'mainlist button[action=addeview]': {
				click: this.addView
			},
			'mainlist button[action=editeview]': {
				click: this.editView
			},
			'mainlist button[action=deleteeview]': {
				click: this.deleteView
			},
			'mainlist button[action=shareeview]': {
				click: this.shareView
			},
			'mainlist': {
				cellclick: this.cellClicked
			},
			 
        });
    
	},
	
	openView: function(btn) {
		var mainGrid = Ext.ComponentQuery.query('mainlist')[0];
		var selection = mainGrid.getView().getSelectionModel().getSelection()[0];
		console.log(selection);
		if(selection) {
			var myMask = Ext.create('Ext.LoadMask',{
				target: mainGrid,
				msg: "Opening, please wait..."
			});
			myMask.show();
			
		} else {
			Ext.Msg.alert('Important', 'Please select an eView first to open.', function(stat){
			});
		}
	},
	
	addView: function(btn) {
		//window.location.href = "../../../myapps/view/querymanager/";
		window.open("../../../myapps/view/querymanager/","_new","height=800,width=900,scrollbars=yes");
	},
	
	editView: function(btn) {
		var mainGrid = Ext.ComponentQuery.query('mainlist')[0];
		var selection = mainGrid.getView().getSelectionModel().getSelection()[0];
		console.log(selection);
		if(selection) {
			var myMask = Ext.create('Ext.LoadMask',{
				target: mainGrid,
				msg: "Opening, please wait..."
			});
			myMask.show();
		} else {
			Ext.Msg.alert('Important', 'Please select an eView first to edit.', function(stat){
			});
		}
	},
	
	deleteView: function(btn) {
		var mainGrid = Ext.ComponentQuery.query('mainlist')[0];
		var selection = mainGrid.getView().getSelectionModel().getSelection()[0];
		console.log(selection);
		if(selection) {
			var myMask = Ext.create('Ext.LoadMask',{
				target: mainGrid,
				msg: "Deleting, please wait..."
			});
			myMask.show();
		} else {
			Ext.Msg.alert('Important', 'Please select an eView first to delete.', function(stat){
			});
		}
	},
	
	shareView: function(btn) {
		var mainGrid = Ext.ComponentQuery.query('mainlist')[0];
		var selection = mainGrid.getView().getSelectionModel().getSelection()[0];
		console.log(selection);
		if(selection) {
			var myMask = Ext.create('Ext.LoadMask',{
				target: mainGrid,
				msg: "Opening, please wait..."
			});
			myMask.show();
		} else {
			Ext.Msg.alert('Important', 'Please select an eView first to share.', function(stat){
			});
		}
	},
	
	cellClicked: function(theCell, td, cellIndex, record, tr, rowIndex) {
		var clickedDataIndex  = theCell.panel.headerCt.getHeaderAtIndex(cellIndex).dataIndex;
        var clickedColumnName = theCell.panel.headerCt.getHeaderAtIndex(cellIndex).text;
        var clickedCellValue  = record.get(clickedDataIndex);
        
        if(clickedDataIndex == 'THENAME') {
        	var viewcode = record.data.EVIEWCODE;
        	var compcode = record.data.COMPANYCODE;
        } else {
        	console.log('not name');
        }
	}
	 
});